const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

const user_id = wx.getStorageSync('user_id')
const jg_stu_id = wx.getStorageSync('jg_stu_id')
// pages/gs/gs_doTi/gs_doTi.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ti_index:0,
    ti_list:[],
  },

  update_gsTi(){
    wx.navigateTo({
      url: '/pages/sysConfig/config_gs/edit_ti/add_ti?info=' + encodeURIComponent(JSON.stringify(this.data.info))
    })
    this.can_onshow = true
  },

  next(){
    if(this.data.ti_index<this.data.ti_list.length-1){
      getApp().get_vibration()
      this.setData({
        info:this.data.ti_list[this.data.ti_index+1],
        ti_index:this.data.ti_index + 1,
      })
      wx.pageScrollTo({
        scrollTop: 0,
        selector: '#top',
      })
    }
  },

  last(){
    if(this.data.ti_index>0){
      getApp().get_vibration()
      this.setData({
        info:this.data.ti_list[this.data.ti_index-1],
        ti_index:this.data.ti_index - 1,
      })
      wx.pageScrollTo({
        scrollTop: 0,
        selector: '#top',
      })
    }
  },

  tap_opt(e){
    getApp().get_vibration()
    let index1 = e.currentTarget.dataset.index1
    let selected = e.currentTarget.dataset.selected
    let info = this.data.info
    var result = selected==info.q_list[index1].answer?1:2
    if(result==1){
      wx.showToast({title: '正确！', duration: 2000, icon: 'success'})
    }else{
      wx.showToast({title: '您答错啦！', duration: 2000, icon: 'error'})
    }

    let form_data = {
      'jg_stu_id':jg_stu_id,
      'gs_stem_id':this.data.info.stem_id,
      'gs_q_id':this.data.info.q_list[index1].id,
      'selected':selected,
      'result':result,
    }
    this.do_Ti_rec(form_data)

    info.q_list[index1].selected = selected
    this.setData({
      info:info
    })
  },


  do_Ti_rec(form_data){
    wx.request({
      url: domainName + '/gs_do_Ti_rec',
      data: {
        form_data:JSON.stringify(form_data)
      },...parameter
    })
  },

  save_storage(){
    getApp().get_vibration()
    wx.showModal({
      cancelText:'不保存',
      confirmText:'保存进度',
      content: '退出前是否保存？',
      showCancel: true,
      title: '提醒',
      success: res => {
        if(res.confirm){
          // 记录上次做题位置
          wx.setStorageSync('gs_doTi_index'+this.type_id, this.data.ti_index)
          wx.setStorageSync('gs_doTi_list'+this.type_id, this.data.ti_list)
        }else{
          wx.removeStorageSync('gs_doTi_index'+this.type_id)
          wx.removeStorageSync('gs_doTi_list'+this.type_id)
        }
      },
    })
  },

  
  page_init(type_id,user_id,ti_index){
    wx.showLoading()
    wx.request({
      url: domainName + '/gs_doTi_init',
      data: {type_id,user_id},...parameter,
      success: res => {
        wx.hideLoading()
        this.setData({
          right_limit:res.data.right_limit,
          ti_list:res.data.lis,
          info:res.data.lis[ti_index],
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log('options',options)
    this.type_id = options.type_id


    let ti_index = wx.getStorageSync('gs_doTi_index'+this.type_id)
    let ti_list = wx.getStorageSync('gs_doTi_list'+this.type_id)
    if(ti_index){
      this.setData({
        ti_list:ti_list,
        ti_index:ti_index,
        info:this.data.ti_list[ti_index],
      })
    }else{
      // 重新加载习题列表
      this.page_init(this.type_id,user_id,0)
      
    }

  },

  onShow(){
    if(this.can_onshow){
      this.page_init(this.type_id, user_id, this.data.ti_index)
      this.can_onshow = false
    }
  },

})